package logic.facade;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import logic.service.GradesService;
import logic.service.GroupService;
import vo.GradesVO;
import vo.IValueObject;

/**
 * @author Alexander
 */
/**
 * Fachada de Calificaciones
 */
public class GradesFacade extends Facade{

/**
 * Construtor Calificaciones
 * Crea un Servicio de Calificaciones.
 */
    public GradesFacade() {
        setService(new GradesService());
    }
    
    @Override
    protected boolean exist(IValueObject vo) {
        return !(super.find(((GradesVO) vo).getId()) == null);
    }
    
    public List<IValueObject> findByUser(String user){
        List<IValueObject> grade = new ArrayList<>();
        EntityManagerFactory emf = null;
        EntityManager em = null;
        
        try{
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            grade = ((GradesService) getService()).findByUser(user, em);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(em != null){
                em.clear();
                em.close();
            }
            return grade;
        }
    }

        public List<GradesVO> gradesList(String user, Integer subjCode){
        List<GradesVO> grades = null;
        EntityManagerFactory emf = null;
        EntityManager em = null;
        
        try{
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            grades = ((GradesService) getService()).gradesList(user, subjCode, em);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(em != null){
                em.clear();
                em.close();
            }
            return grades;
        }
    }
    
}
